package com.woniu.atms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.atms.entity.G5Passenger;
import com.woniu.atms.entity.G5User;
import com.woniu.atms.exception.UserNameExistException;
import com.woniu.atms.mapper.G5UserDao;
import com.woniu.atms.service.G5UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * (G5User)表服务实现类
 *
 * @author makejava
 * @since 2022-01-18 17:24:38
 */
@Service("g5UserService")
public class G5UserServiceImpl extends ServiceImpl<G5UserDao, G5User> implements G5UserService {
    @Resource
    private G5UserDao g5UserDao;

    /**
     * 通过激活码查找单条数据
     *
     * @param vCode
     * @return
     */
    @Override
    public G5User findUserByVCode(String vCode) {
        return g5UserDao.findUserByVCode(vCode);
    }

    /**
     * 通过账号名查找单条数据
     *
     * @param account
     * @return
     */
    @Override
    public G5User findUserByAccount(String account) {
        return g5UserDao.findUserByAccount(account);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public G5User queryById(Integer id) {
        return this.g5UserDao.queryById(id);
    }

    /**
     * 按条件分页查询
     *
     * @param pageNum   起始页
     * @param condition 筛选条件
     * @return
     */
    @Override
    public PageInfo<G5User> findByPage(Map<String, Object> condition, Integer pageNum, Integer pageSize) {
        //设置页码和每页记录数
        PageHelper.startPage(pageNum, pageSize);
        //获取集合
        List<G5User> g5UserList = g5UserDao.queryByCondition(condition);
        //创建分页对象
        PageInfo<G5User> pageInfo = new PageInfo<>(g5UserList);
        //返回分页对象
        return pageInfo;
    }

    /**
     * 新增数据
     *
     * @param g5User 实例对象
     * @return 实例对象
     */
    @Override
    public G5User add(G5User g5User) {
        //判断用户是否存在，不能加入重复用户
//        if (g5UserDao.getNumOfAccount(g5User.getAccount()) == 1) {
//            throw new UserNameExistException("用户名：" + g5User.getAccount() + "已存在！");
//        }
//        if (g5UserDao.getNumOfTelephone(g5User.getTelephone()) == 1) {
//            throw new UserNameExistException("手机号：" + g5User.getTelephone() + "已存在！");
//        }
//        if (g5UserDao.getNumOfEmail(g5User.getEmail()) == 1) {
//            throw new UserNameExistException("邮箱：" + g5User.getEmail() + "已存在！");
//        }
        g5User.setAvatar("");
        g5User.setStatus("n");
        g5User.setErrorNum(0);
        g5User.setRefundNum(0);
        g5User.setIsExit("y");
        this.g5UserDao.insert(g5User);
        return g5User;
    }

    /**
     * 修改数据
     *
     * @param g5User 实例对象
     * @return 实例对象
     */
    @Override
    public G5User update(G5User g5User) {
        this.g5UserDao.update(g5User);
        return this.queryById(g5User.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.g5UserDao.deleteById(id) > 0;
    }
}
